<template>
  <div>
    <el-page-header @back="goBack" content="提出需求">
    </el-page-header>

    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="需求名称" prop="reqName">
        <el-input v-model="ruleForm.reqName"></el-input>
      </el-form-item>
      <el-form-item label="项目名称" prop="proName">
        <el-select v-model="ruleForm.proName" placeholder="请选择">
          <el-option
              v-for="item in projectListOptions"
              :key="item.id"
              :label="item.proName"
              :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="负责人" prop="distributed">
        <el-input v-model="ruleForm.distributed"></el-input>
      </el-form-item>
      <el-form-item label="需求描述" prop="reqDescription">
        <el-input type="textarea" :rows="5" v-model="ruleForm.reqDescription"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      projectListOptions: [],
      ruleForm: {
        userId: '',
        projectId: '',
        reqName: '',
        distributed: '',
        reqDescription: ''
      },
      rules: {
        reqName: [
          {required: true, message: '请输入需求名称,方便查找', trigger: 'blur'},
          {min: 4, max: 50, message: '长度在 4 到 50 个字符', trigger: 'blur'}
        ],
        distributed: [
          {required: true, message: '请输入负责人', trigger: 'blur'},
          {min: 4, max: 50, message: '长度在 4 到 50 个字符', trigger: 'blur'}
        ],
        reqDescription: [
          {required: true, message: '请对需求进行描述', trigger: 'blur'},
          {max: 500, message: '长度在500个字符以内', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    goBack() {
      this.$router.push({path: '/req'})
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:8989/reqs/insert';
          console.log('url = ' + url);
          let formData = this.qs.stringify(this.ruleForm, {arrayFormat: 'repeat'});
          console.log('formData = ' + formData);
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            console.log('responseBody = ');
            console.log(responseBody);
            if (responseBody.state == 20000) {
              this.$message({
                message: '提交需求成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    loadProjectList() {
      console.log('loadProjectList');
      let url = 'http://localhost:8989/projects/list';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        this.projectListOptions = responseBody.data;
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.loadProjectList();
  }
}
</script>